#!/usr/bin/perl
use FindBin;
use lib "$FindBin::Bin/lib";
use lib "$FindBin::Bin/lib/perl-lib/lib/perl5";

use IO::File;
use strict;
use Getopt::Long;
use Cwd 'abs_path';
use File::Basename;
use POSIX;
use Patcher;

Getopt::Long::Configure("pass_through");

my $homePath = abs_path("$FindBin::Bin/..");
my $progName = $FindBin::Script;
my @uname    = uname();
my $osType   = 'unix';
$osType = 'windows' if ( $uname[0] =~ /Windows/i );

sub usage {
    print("Usage:$progName --ins INSName --version VERSION\n");
    print("--backupdir BackupSaveDir --backupcount MinBackupCount --backuplastdays BackupMinDays\n");
    print(" --versrcpath SrcPath --targetdir TargetDir\n");
    exit(-1);
}

sub main {
    if ( $osType eq 'windows' ) {
        $ENV{PATH} = "$homePath/../7-Zip;$ENV{ProgramFiles}/7-Zip;" . $ENV{PATH};
    }

    my $isHelp;
    my $isVerbose = 0;
    my $insName;
    my $version;
    my $verSrcPath;
    my $backupDir;
    my $backupCount    = 5;
    my $backupLastDays = 90;
    my $targetDir;

    GetOptions(
        'h|help'           => \$isHelp,
        'v｜verbose=i'      => \$isVerbose,
        'ins=s'            => \$insName,
        'version=s'        => \$version,
        'backupcount=i'    => \$backupCount,
        'backuplastdays=i' => \$backupLastDays,
        'versrcpath=s'     => \$verSrcPath,
        'backupdir=s'      => \$backupDir,
        'targetdir=s'      => \$targetDir
    );

    usage() if ( defined($isHelp) );

    my $backupType = 'fullbackup';

    my $patchSrcPath = $verSrcPath;

    my $hasOptionError = 0;
    if ( not defined($version) or $version eq '' ) {
        $hasOptionError = 1;
        print("ERROR: Version not defined(option --version).\n");
    }

    if ( not defined($targetDir) or $targetDir eq '' ) {
        $hasOptionError = 1;
        print("ERROR: Target directory not defined(option --targetdir).\n");
    }
    elsif ( not -e $targetDir ) {
        $hasOptionError = 1;
        print("ERROR: Target directory:$targetDir not exist.\n");
    }

    if ( not defined($patchSrcPath) or $patchSrcPath eq '' ) {
        $hasOptionError = 1;
        print("ERROR: Version resource directory not defined(option --versrcpath).\n");
    }
    elsif ( not -e $patchSrcPath ) {
        $hasOptionError = 1;
        print("ERROR: Version resource directory:$patchSrcPath not exist.\n");
    }

    if ( not defined($backupDir) or $backupDir eq '' ) {
        $hasOptionError = 1;
        print("ERROR: Backup directory not defined(option --backupdir).\n");
    }
    else {
        my $backupParent = dirname($backupDir);
        my $backupRoot   = dirname($backupParent);
        if ( not -d $backupRoot ) {
            $hasOptionError = 1;
            print("ERROR: Backup directory:$backupRoot not exist.\n");
        }
        else {
            mkdir($backupParent);
        }
    }

    if ( $hasOptionError == 1 ) {
        usage();
    }

    my $patcher = Patcher->new( $homePath, $backupDir, $backupCount, $backupLastDays );
    my $status  = 0;
    $status = $patcher->backup( $insName, $version, $patchSrcPath, $targetDir, $backupType );

    if ( $status == 0 ) {
        $status = $patcher->deploy( $insName, $version, $patchSrcPath, $targetDir, 1 );
    }

    if ( $status == 0 ) {
        print("INFO: Deploy $version to $insName with $verSrcPath success.\n");
    }
    else {
        print("ERROR: Deploy $version to $insName with $verSrcPath failed.\n");
    }

    return $status;
}

exit main();

